<?php

	namespace Home\model;
	use Think\Model;
	
	class ImageModel extends Model
	{
		// 处理详情页数据
		public function showdetail()
		{
			// 获取ID
			$id = I('get.id');

			// 遍历的评价
			$eva = M('goods_evaluate');
			$evalist = $eva->where("goods_id = {$id}")->select();
			$gg = M('goods');
			$gname = $gg->field('name')->where("goods_id = {$id}")->find()['name'];
			
			$a = count($evalist);
			$sum = 0;
			foreach ($evalist as $evakey => $evavalue) {
				$sum += $evavalue['startlevel'];
			}
			$sum = ceil($sum / $a);	
			
			foreach ($evalist as $evalistkey => &$evalistval) {
				$evalistval['addtime'] = date('Y-m-d' , $evalistval['addtime']);
			}

			// 查询商品
			$good = M('goods');
			$goods = $good->where('goods_id = ' . $id)->select();

			$name = $goods[0]['name'];
			// 获取相同的
			$sql = "select * from `north_goods`  where `name` = '$name' and `goods_id` not in ($id)";

			$samegoods = $good->query($sql);
			$samegoods = array_merge($goods,$samegoods);


			foreach ($samegoods as $key => &$value) {

				// 切割价格的名字
				$value['price'] = explode("." , $value['price']);

				// 获取图片的路径
				$img = M('image');
				$goods_img = $img->field('path , name')->where("goods_id =  {$value['goods_id']} and status in (1)")->select();
				foreach ($goods_img as $kk => &$imgval) {
					$imgval['path'] = ltrim($imgval['path'] , './');
				}
				$value['img'] = $goods_img;

				// 获取尺码和对应的数量
				$attr = M('attr');
				$attrlist = $attr->where('goods_id = ' . $value['goods_id'])->select();
				// 遍历属性表
				foreach ($attrlist as $ke => $val) {
					$value['store'][$ke] = $val['store'];
					$detail = M('details');

					$detaillist = $detail->where('details_id = ' . $val['details_id'] )->select();
					// 遍历尺码表
					foreach ($detaillist as $k => $v) {
						// $value['details_id'][$ke] = $v['details_id'];
						$value['size'][$ke] = $v['size'];
					}
					
				}

			}
			// echo $evacount;
			return array('samegoods' => $samegoods , 'evalist' => $evalist , 'a' => $a , 'gname' => $gname , 'sum' => $sum);
		}


	}
